package JZOffer;

import java.util.Stack;

/**
 * @author Lw中
 * @date 2021/9/14 20:50
 */
public class Jz31 {

    // Jz31. 栈的压入、弹出序列
    public boolean validateStackSequences(int[] pushed, int[] popped) {
        // 使用辅助栈进行存储popped数组中的元素
        Stack<Integer> stack = new Stack<>();
        int i = 0;
        for (int num : pushed) {
            stack.push(num);
            while (!stack.isEmpty() && stack.peek() == popped[i]) {
                stack.pop();
                i++;
            }
        }
        return stack.isEmpty();
    }

}
